package controller;

import dao.AreaJpaController;
import dao.exceptions.RollbackFailureException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.exceptions.NonexistentEntityException;
import model.Area;
import pagecontroller.JsfUtil;
import pagecontroller.PageManager;

/**
 *
 * @author Edilson Fernandes da Costa
 * @version 1.0
 */
public class AreaController {

    public AreaController() {
        if (JsfUtil.getObjectFromSession("AreaJpaController") == null) {
            jpaController = new AreaJpaController();
        } else {
            jpaController = (AreaJpaController) JsfUtil.getObjectFromSession("AreaJpaController");
        }
    }
    private Area area = null;
    private Area areaAux = null;
    private AreaJpaController jpaController = null;
    private List<Area> areas = null;


   public String persiste() {
        if (area.getId() == null) {
            return create();
        } else {
            return update();
        }
    }

    public String update() {
        try {
            jpaController.edit(this.area);
            areaAux = area;
            this.area = null;
            JsfUtil.addSuccessMessage("Area Atualizado com Sucesso");
            getPageManager().setPageAreaRelat();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }

    public String create() {
        try {
            jpaController.create(area);
            areaAux = area;
            area = null;
            JsfUtil.addSuccessMessage("Area cadastrado com sucesso.");
            getPageManager().setPageAreaRelat();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "Ocorreu um erro na percistencia dos dados.");
            return null;
        }
        return "refreshPage";
    }

    public String edit() {
        Area areaTemp = (Area) JsfUtil.getObjectFromRequestParameter("areaTemp");
        this.area = areaTemp;
        return "refreshPage";
    }

    public String load() {
        areaAux = (Area) JsfUtil.getObjectFromRequestParameter("areaTemp");
        getPageManager().setPageAreaRelat();
        return "refreshPage";
    }


    public String destroy() {
        Area areaTemp = (Area) JsfUtil.getObjectFromRequestParameter("area");
        try {
            jpaController.destroy(areaTemp.getId());
            JsfUtil.addSuccessMessage("Area removido com sucesso.");
        }  catch (NonexistentEntityException ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        } 

        return "refreshPage";
    }


    public String editButton() {
        this.area = areaAux;
        getPageManager().setPageAreaCad();

        return "refreshPage";
    }

    public String destroyButton() {
        try {
            Area areaTemp = areaAux;
            jpaController.destroy(areaTemp.getId());
            JsfUtil.addSuccessMessage("Area removida com sucesso.");
            areaAux = null;
            
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }


    private PageManager getPageManager() {
        PageManager pageManager = (PageManager) JsfUtil.getObjectFromSession("PageManager");
        return pageManager;
    }



    public Area getArea() {
        if (area == null) {
            area = new Area();
        }
        return area;
    }

    public List<Area> getAreas(){
        this.areas = jpaController.findAreaEntities();
        return this.areas;
    }

    public AreaJpaController getJpaController() {
        return jpaController;
    }

    public Area getAreaAux() {
        return areaAux;
    }


}
